package c.a.md.androidvideo.widget;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.io.InputStream;

/**
 * Created by ytf on 2018/10/15.
 * Description:
 */
public class NormalImageViewDisplay extends AbsView
{

    Bitmap bmp;

    public NormalImageViewDisplay(Context context)
    {
        this(context, null);
    }

    public NormalImageViewDisplay(Context context, @Nullable AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public NormalImageViewDisplay(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context)
    {
        try
        {
            AssetManager am = getResources().getAssets();
            InputStream is = am.open("lbxx.png");
            bmp = BitmapFactory.decodeStream(is);
        }catch (Exception e)
        {

        }
    }

    @Override
    public int getDefaultWrapWidth()
    {
        return 100;
    }

    @Override
    public int getDefaultWrapHeight()
    {
        return 100;
    }

    @Override
    public void onGlobalLayout()
    {

    }


    private Matrix mx1;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        mx1 = new Matrix();

        int bw = bmp.getWidth();
        int bh = bmp.getHeight();
        float scale;
        if(bw > bh)
        {
            scale = w * 1.0f / bw;
            mx1.setScale(scale, scale);
            float py = (h - scale * bh) / 2;
            mx1.postTranslate(0, py);
        }else
        {
            scale = h * 1.0f / bh;
            mx1.setScale(scale, scale);
            float px = (w - scale * bw) / 2;
            mx1.postTranslate(px, 0);
        }
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(bmp, mx1, null);
    }
}
